// PV_DB Translator
// Copyright (C) 2019 nastys
//
// This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License along with this program. If not, see <http://www.gnu.org/licenses/>.

#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QFile>
#include <QTextStream>
#include <QMessageBox>
#include <QFileDialog>

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::on_pushButton_Copy_clicked()
{
    QFile copyFrom(ui->lineEdit_copyFrom->text()), copyTo(ui->lineEdit_copyTo->text()), saveAs(ui->lineEdit_saveAs->text());
    if (!(copyFrom.open(QIODevice::ReadOnly)))
    {
        QMessageBox::critical(this, "Error", "Could not open file (from).");
        return;
    }

    if (!(copyTo.open(QIODevice::ReadOnly)))
    {
        QMessageBox::critical(this, "Error", "Could not open file (to).");
        return;
    }

    saveAs.remove();

    if (!(saveAs.open(QIODevice::ReadWrite)))
    {
        QMessageBox::critical(this, "Error", "Could not open file (output).");
        return;
    }

    QTextStream streamFrom(&copyFrom), streamTo(&copyTo), streamOut(&saveAs);
    streamFrom.setCodec("UTF-8");
    streamTo.setCodec("UTF-8");
    streamOut.setCodec("UTF-8");

    while(!streamTo.atEnd())
    {
        // Read the next line in the target file
        QString line_to = streamTo.readLine();

        // If it contains ".lyric.", etc...
        if ( (line_to.contains(".lyric.") || line_to.contains(".song_name=") || line_to.contains(".songinfo.") || line_to.contains(".name=") ) )
        {
            bool found=0;
            while(!streamFrom.atEnd()&&found==0)
            {
                // Read the next line in the translation file
                QString line_from = streamFrom.readLine();

                // If the lines match
                if (line_from.split("=").at(0) == line_to.split("=").at(0))
                {
                    // Copy the line from the translation file
                    streamOut << line_from << endl;
                    found=1;
                }

                // If nothing could be found, copy the line from the target file
                if (streamFrom.atEnd()&&found==0)
                    streamOut << line_to << endl;
            }

            // Reset translation file
            copyFrom.seek(0);
            streamFrom.reset();
            streamFrom.resetStatus();
        }
        else streamOut << line_to << endl;
    }

    // Reset target file
    copyTo.seek(0);
    streamTo.reset();
    streamTo.resetStatus();

    copyFrom.close(); copyTo.close(); saveAs.close();

    QMessageBox::information(this, "Yay", "Done!");
}

void MainWindow::on_licence_clicked()
{
    QMessageBox::information(this, "Licence", "PV_DB Translator\nCopyright (C) 2019 nastys\n\nThis program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.\n\nThis program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.\n\nYou should have received a copy of the GNU General Public License along with this program. If not, see <http://www.gnu.org/licenses/>.");
}

void MainWindow::on_pushButton_copyFrom_clicked()
{
    QString copyFrom_string = QFileDialog::getOpenFileName(this, "Open old, translated pv_db");
    if (copyFrom_string != "")
        ui->lineEdit_copyFrom->setText(copyFrom_string);
}

void MainWindow::on_pushButton_copyTo_clicked()
{
    QString copyTo_string = QFileDialog::getOpenFileName(this, "Open new, untranslated pv_db");
    if (copyTo_string != "")
        ui->lineEdit_copyTo->setText(copyTo_string);
}

void MainWindow::on_pushButton_saveAs_clicked()
{
    QString saveAs_string = QFileDialog::getSaveFileName(this, "Save new, partially translated pv_db");
    if (saveAs_string != "")
        ui->lineEdit_saveAs->setText(saveAs_string);
}
